package com.ynet.middleground.riskmanage.service.impl;

import java.util.List;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.ynet.middleground.riskmanage.bean.*;
import com.ynet.middleground.riskmanage.dto.CustListRespDTO;
import com.ynet.middleground.riskmanage.bean.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.riskmanage.bean.BlchInReq;
import com.ynet.middleground.riskmanage.bean.BlchOutReq;
import com.ynet.middleground.riskmanage.bean.BlchQueryReq;
import com.ynet.middleground.riskmanage.bean.PayableQueryReq;
import com.ynet.middleground.riskmanage.dto.BlchRespDTO;
import com.ynet.middleground.riskmanage.dto.BlchStatusDTO;
import com.ynet.middleground.riskmanage.dto.PayableDTO;
import com.ynet.middleground.riskmanage.model.worldex.WorldexModel;
import com.ynet.middleground.riskmanage.service.WorldexService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author liulx
 * @description 全球捷运接口服务
 * @date 2020-08-17 14:52
 */
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Api(value = "全球捷运接口服务", tags = {"全球捷运接口服务"})
@Path("worldex")
@Service(timeout = 6000, version = "0.0.1")
public class WorldexServiceImpl implements WorldexService, GenericService {

    @Autowired
    private WorldexModel worldexModel;

    /**
     * 查询应付账款列表
     *
     * @param req 应付账查询信息
     * @return 应付账款列表
     */
    @POST
    @Path("payable")
    @ApiOperation(value = "查询应付账款信息列表", notes = "查询用户在全球捷运系统中的应付账款信息列表")
    @ApiImplicitParam(name = "requestObj", dataType = "PayableQueryReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<PayableDTO>> listServices(@Valid PayableQueryReq req) {
        ServiceResult<List<PayableDTO>> result = new ServiceResult<>();
        try {
            result.setResult(worldexModel.listPayable(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询应付账款信息列表服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 查询保理池业务出入池状态
     *
     * @param req 查询保理池业务出入池状态请求信息
     * @return 出入池状态数据
     */
    @POST
    @Path("status")
    @ApiOperation(value = "查询保理池业务出入池状态", notes = "查询在保理池业务中指定数据的出入池状态")
    @ApiImplicitParam(name = "requestObj", dataType = "BlchQueryReq", paramType = "body", required = true)
    @Override
    public ServiceResult<BlchStatusDTO> queryBlchStatus(@Valid BlchQueryReq req) {
        ServiceResult<BlchStatusDTO> result = new ServiceResult<>();
        try {
            result.setResult(worldexModel.queryBlchStatus(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询保理池业务出入池状态服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 接收全球捷运推送的保理池入池数据
     *
     * @param req 保理池入池数据参数信息
     * @return
     */
    @POST
    @Path("qqjy-to-jkztfk-blch-in")
    @ApiOperation(value = "接收全球捷运推送的保理池入池数据", notes = "仅供全球捷运调用，推送保理池业务<b>入池数据</b>")
    @ApiImplicitParam(name = "requestObj", dataType = "BlchInReq", paramType = "body", required = true)
    @Override
    public ServiceResult<BlchRespDTO> receiveBlchIn(@Valid BlchInReq req) {
        ServiceResult<BlchRespDTO> result = new ServiceResult<>();
        try {
            result.setResult(worldexModel.saveBlchIn(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "接收全球捷运推送的保理池入池数据服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 接收全球捷运推送的保理池出池数据
     *
     * @param req 保理池出池数据参数信息
     * @return
     */
    @POST
    @Path("qqjy-to-jkztfk-blch-out")
    @ApiOperation(value = "接收全球捷运推送的保理池出池数据", notes = "仅供全球捷运调用，推送保理池业务<b>出池数据</b>")
    @ApiImplicitParam(name = "requestObj", dataType = "BlchOutReq", paramType = "body", required = true)
    @Override
    public ServiceResult<BlchRespDTO> receiveBlchOut(@Valid BlchOutReq req) {
        ServiceResult<BlchRespDTO> result = new ServiceResult<>();
        try {
            result.setResult(worldexModel.saveBlchOut(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "接收全球捷运推送的保理池出池数据服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 客户名单查询
     *
     * @param req
     * @return
     */
    @POST
    @Path("getCustList")
    @ApiOperation(value = "客户名单查询", notes = "客户名单查询")
    @ApiImplicitParam(name = "requestObj", dataType = "CustListQueryReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<CustListRespDTO>> getCustList(CustListQueryReq req) {
        ServiceResult<List<CustListRespDTO>> result = new ServiceResult<>();
        try {
            result.setResult(worldexModel.getCustList(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "客户名单查询服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 客户审核结果推送
     *
     * @param req
     * @return
     */
    @POST
    @Path("pushCustApproveResult")
    @ApiOperation(value = "客户审核结果推送", notes = "客户审核结果推送")
    @ApiImplicitParam(name = "requestObj", dataType = "CustApproveListReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Object> pushCustApproveResult(CustApproveListReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            result.setSuccess(worldexModel.pushCustApproveResult(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
            result.setSuccess(false);
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "客户审核结果推送服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 接收全球捷运推送的客户分析数据
     *
     * @param req 客户分析数据参数信息
     * @return 响应结果数据
     */
    @POST
    @Path("customer-analytics")
    @ApiOperation(value = "接收全球捷运推送的客户分析数据", notes = "仅供全球捷运调用，接收全球捷运推送的客户分析数据")
    @ApiImplicitParam(name = "requestObj", dataType = "WorldexCustomerAnalyticsReq", paramType = "body",
        required = true)
    @Override
    public ServiceResult<String> receiveCustomerAnalytics(WorldexCustomerAnalyticsReq req) {
        ServiceResult<String> serviceResult = new ServiceResult<>();
        String returnsResult = null;
        try {
            returnsResult = worldexModel.receiveCustomerAnalytics(req);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "全球捷运客户分析数据服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        serviceResult.setResult(returnsResult);
        return serviceResult;
    }

    /**
     * 接收全球捷运推送的客户全量数据
     *
     * @param req 客户全量数据参数信息
     * @return 响应结果数据
     */
    @POST
    @Path("all-customer")
    @ApiOperation(value = "接收全球捷运推送的全量客户数据", notes = "仅供全球捷运调用，接收全球捷运推送的全量客户数据")
    @ApiImplicitParam(name = "requestObj", dataType = "WorldexCustomerAllReq", paramType = "body", required = true)
    @Override
    public ServiceResult<String> receiveCustomerAll(WorldexCustomerAllListReq req) {
        ServiceResult<String> serviceResult = new ServiceResult<>();
        String returnsResult = null;
        try {
            returnsResult = worldexModel.receiveCustomerAll(req);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "全球捷运客户全量数据服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        serviceResult.setResult(returnsResult);
        return serviceResult;
    }

}
